package com.linkage.lcims.base.memoryparam.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.linkage.lcims.base.dao.business.interfaces.TollAttrPolicyDAO;
import com.linkage.lcims.base.dao.business.interfaces.TollPolicyAttrDAO;
import com.linkage.lcims.base.dao.business.interfaces.TollPolicyDAO;
import com.linkage.lcims.base.dao.business.interfaces.TollPolicySuitDAO;
import com.linkage.lcims.base.memoryparam.MemoryParamImpl;
import com.linkage.lcims.base.memoryparam.interfaces.TollPolicyMP;
import com.linkage.lcims.base.po.business.BdGroupId;
import com.linkage.lcims.base.po.business.BdTollAttrPolicy;
import com.linkage.lcims.base.po.business.BdTollPolicy;
import com.linkage.lcims.base.po.business.BdTollPolicyAttr;
import com.linkage.lcims.base.po.business.BdTollPolicySuit;
import com.linkage.util.exception.MPException;

public class TollPolicyMPImpl extends MemoryParamImpl<BdTollPolicy> implements
		TollPolicyMP {

	private static TollPolicyMPImpl m_instance = null;
	
	private TollPolicyDAO tollPolicyDAO;
	private TollAttrPolicyDAO tollAttrPolicyDAO;
	private TollPolicyAttrDAO tollPolicyAttrDAO;
	private TollPolicySuitDAO tollPolicySuitDAO;
	
	private List<BdTollPolicy> allTollPolicy;
	
	private List<BdTollAttrPolicy> allTollAttrPolicy;
	private List<BdTollPolicyAttr> allTollPolicyAttr;
	private List<BdTollPolicySuit> allTollPolicySuit;
 	
	
	private TollPolicyMPImpl() {
	}
	
	public static TollPolicyMPImpl getInstance() {
		if (m_instance == null) {
			m_instance = new TollPolicyMPImpl();
		}
		return m_instance;
	}

	public void reload() throws Exception {
		try {
			allTollPolicy = tollPolicyDAO.getAll();
			allTollAttrPolicy=tollAttrPolicyDAO.getAll();
			allTollPolicyAttr=tollPolicyAttrDAO.getAll();
			allTollPolicySuit=tollPolicySuitDAO.getAll();
			this.setReload(false);
		} catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollPolicy> getAllTollPolicy() throws MPException {
		try {
			if (isReload())
				reload();
			return allTollPolicy;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public BdTollPolicy getTollPolicyByID(Integer policyid) throws MPException {
		// TODO Auto-generated method stub
		try {
			if (isReload())
				reload();
			if(allTollPolicy != null && allTollPolicy.size() > 0){
				Iterator<BdTollPolicy> iter = allTollPolicy.iterator();
				BdTollPolicy tollPolicy;
				while (iter.hasNext()){
					tollPolicy = iter.next();
					if (tollPolicy.getPolicyid().intValue() == policyid.intValue()) {
						return tollPolicy;
					}
				}
			}
			return null;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}
	
	public String getPolicyNameByID(Integer policyid) throws MPException {
		try {			
			BdTollPolicy tollpolicy = getTollPolicyByID(policyid);
			if (tollpolicy != null)
				return tollpolicy.getPolicyname();
			else
				return (String) null;
		} catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public TollPolicyDAO getTollPolicyDAO() {
		return tollPolicyDAO;
	}

	public void setTollPolicyDAO(TollPolicyDAO tollPolicyDAO) {
		this.tollPolicyDAO = tollPolicyDAO;
	}

	public List<BdTollAttrPolicy> getAllTollAttrPolicy() throws MPException {
		try {
			if (isReload())
				reload();
			return allTollAttrPolicy;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollPolicyAttr> getAllTollPolicyAttr() throws MPException {
		try {
			if (isReload())
				reload();
			return allTollPolicyAttr;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollPolicySuit> getAllTollPolicySuit() throws MPException {
		try {
			if (isReload())
				reload();
			return allTollPolicySuit;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollPolicy> getAll() throws Exception {
		try {
			if (isReload())
				reload();
			return allTollPolicy;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollAttrPolicy> getTollAttrPolicyByGroupid(int groupid)
			throws MPException {
		try {
			BdGroupId bdGroup=GroupIDMPImpl.getInstance().getGroupID(groupid);
			if(bdGroup==null)
				return null;
			int policyid=bdGroup.getPolicyid();
			List<BdTollAttrPolicy> retList=new ArrayList<BdTollAttrPolicy>();
			List<BdTollAttrPolicy> cacheList=this.getAllTollAttrPolicy();
			for(BdTollAttrPolicy tmp:cacheList){
				if(tmp.getComp_id().getPolicyid().intValue()==policyid){
					retList.add(tmp);
				}
			}
			return retList;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollPolicyAttr> getTollPolicyAttrByPolicyidAndRecordid(
			int policyid, int recordid) throws MPException {
		try {
			List<BdTollPolicyAttr> retList=new ArrayList<BdTollPolicyAttr>();
			for(BdTollPolicyAttr tmp:this.getAllTollPolicyAttr()){
				if(tmp.getComp_id().getPolicyid().intValue()==policyid
						&&tmp.getComp_id().getRecordid().intValue()==recordid){
					retList.add(tmp);
				}
			}
			return retList;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public List<BdTollPolicyAttr> getTollPolicyAttrByTollAttrPolicy(
			BdTollAttrPolicy tollAttrPolicy) throws MPException {
		// TODO Auto-generated method stub
		if(tollAttrPolicy==null||tollAttrPolicy.getComp_id()==null)
			return null;
		return this.getTollPolicyAttrByPolicyidAndRecordid(
				tollAttrPolicy.getComp_id().getPolicyid(), 
				tollAttrPolicy.getComp_id().getRecordid());
	}

	public List<BdTollPolicySuit> getTollPolicySuitByPolicyidAndRecordid(
			int policyid, int recordid) throws MPException {
		List<BdTollPolicySuit> retList=new ArrayList<BdTollPolicySuit>();
		for(BdTollPolicySuit tmp:this.getAllTollPolicySuit()){
			if(tmp.getComp_id().getPolicyid().intValue()==policyid
					&&tmp.getComp_id().getRecordid().intValue()==recordid){
				retList.add(tmp);
			}
		}
		return retList;
	}

	public List<BdTollPolicySuit> getTollPolicySuitByTollAttrPolicy(
			BdTollAttrPolicy tollAttrPolicy) throws MPException {
		if(tollAttrPolicy==null||tollAttrPolicy.getComp_id()==null){
			return null;
		}
		
		return this.getTollPolicySuitByPolicyidAndRecordid(
				tollAttrPolicy.getComp_id().getPolicyid(),
				tollAttrPolicy.getComp_id().getRecordid());
	}

	public BdTollPolicy getTollPolicyByGroupid(int groupid) throws MPException {
		try {
			BdGroupId bdGroup=GroupIDMPImpl.getInstance().getGroupID(groupid);
			if(bdGroup==null)
				return null;
			int policyid=bdGroup.getPolicyid();
			List<BdTollPolicy> cacheList=this.getAllTollPolicy();
			for(BdTollPolicy tmp:cacheList){
				if(tmp.getPolicyid().intValue()==policyid){
					return tmp;
				}
			}
			return null;
		}catch (Exception e) {
			throw new MPException(e, this.getClass());
		}
	}

	public TollAttrPolicyDAO getTollAttrPolicyDAO() {
		return tollAttrPolicyDAO;
	}

	public void setTollAttrPolicyDAO(TollAttrPolicyDAO tollAttrPolicyDAO) {
		this.tollAttrPolicyDAO = tollAttrPolicyDAO;
	}

	public TollPolicyAttrDAO getTollPolicyAttrDAO() {
		return tollPolicyAttrDAO;
	}

	public void setTollPolicyAttrDAO(TollPolicyAttrDAO tollPolicyAttrDAO) {
		this.tollPolicyAttrDAO = tollPolicyAttrDAO;
	}

	public TollPolicySuitDAO getTollPolicySuitDAO() {
		return tollPolicySuitDAO;
	}

	public void setTollPolicySuitDAO(TollPolicySuitDAO tollPolicySuitDAO) {
		this.tollPolicySuitDAO = tollPolicySuitDAO;
	}

}
